# -*- coding: utf-8 -*-

"""
DateTime   : 2020/12/06 9:43
Author     : ZhangYafei
Description:
图书管理系统
    1.查询图书
    2.增加图书
    3.借阅图书
    4.归还图书
    5.退出系统
"""
#书：书名、作者、状态、位置
"""
c        谭浩强   未借出  ISO9001
python   guido   未借出   NFS8102
java     westos  未借出  PKI7844
"""

class Book(object):
    def __init__(self,name,author,status,bookindex):
        self.name = name
        self.author = author
        self.status = status
        self.bookindex = bookindex

    def __str__(self):
        if self.status == 1:
            stats = '未借出'
        elif self.status == 0:
            stats = '已借出'
        else:
            stats = '未知状态'
        return '书名:《%s》 作者: %s 状态: <%s> 位置: %s' \
        %(self.name,self.author,stats,self.bookindex)

class BooKManager(object):
    books = []

    def Start(self):
        self.books.append(Book('c','谭浩强',1,'ISO9001'))
        self.books.append(Book('python','guido',1,'NFS8102'))
        self.books.append(Book('java','westos',1,'PKI7844'))

    def Menu(self):
        self.Start()
        while True:
            print("""图书管理系统\n\t1.查询图书\n\t2.增加图书\n\t3.借阅图书\n\t4.归还图书\n\t5.退出系统""")

            choice = input('输入您的选择: ')
            if choice == '1':
                self.ShowAllBook()
            elif choice == '2':
                self.AddBook()
            elif choice == '3':
                self.BorrowBook()
            elif choice == '4':
                self.ReturnBook()
            elif choice == '5':
                print('欢迎下次使用...')
                exit()
            else:
                print('请输入正确选择！')
                continue

    def CheckBook(self,name):
        for book in self.books:
            if book.name == name:
                return book
        else:
            return None

    def ShowAllBook(self):
        for book in self.books:
            print(book)

    def AddBook(self):
        name = input('图书名称: ')
        self.books.append(Book(name,input('作者:'),1,input('存储位置:')))
        print('图书《%s》增加成功' %name)

    def BorrowBook(self):
        name = input('借阅图书名称: ')
        ret = self.CheckBook(name)
        print(ret)

        if ret != None:
            if ret.status == 0:
                print('书籍《%s》已经借出' %name)
            else:
                ret.status = 0
                print('书籍《%s》借阅成功' %name)
        else:
            print('书籍《%s》不存在' %name)

    def ReturnBook(self):
        name = input('归还图书名称: ')
        ret = self.CheckBook(name)

        if ret != None:
            if ret.status == 0:
                ret.status = 1
                print('书籍《%s》归还成功' %name)
                print(ret)
            else:
                print('书籍《%s》未借出' %name)
        else:
            print('书籍《%s》不存在' %name)

manage = BooKManager()
manage.Menu()